package com.cloudeye.business.mq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanExpressionContext;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

/**
 * @Author: lafangyuan
 * @Date: 2021/12/7 10:21
 */
@Component
@Slf4j
public class RabbitMqStartRunner implements ApplicationRunner {

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Autowired
    private ConfigurableBeanFactory beanFactory;

    @Override
    public void run(ApplicationArguments args) throws Exception {
      createExchange();
      registerToContext();
    }
    private void createExchange(){
        RabbitMqMsgTypeEnum[] types = RabbitMqMsgTypeEnum.values();
        for(RabbitMqMsgTypeEnum type:types){
            rabbitAdmin.declareExchange(new TopicExchange(type.getExchange()));
            log.info("创建Exchange成功:{}",type.getExchange());
        }
    }
    private void registerToContext(){
//        RabbitMqMsgTypeEnum[] types = RabbitMqMsgTypeEnum.values();
//        String rootClassName =  RabbitMqMsgTypeEnum.class.getName();
//        for(RabbitMqMsgTypeEnum type:types){
//            beanFactory.registerSingleton(rootClassName+"."+type.toString()+".exchange",type.getExchange().toString());
//            beanFactory.registerSingleton(rootClassName+"."+type.toString()+".routingKey",type.getRoutingKey().toString());
//        }
    }
}
